package org.spolo.android.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.spolo.android.bean.Vector2;
import org.spolo.android.nodetree.Stage;
import org.spolo.android.nodetree.Vcanvas;
import org.spolo.android.shape.Hotpoint;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class MovieClip extends Vcanvas {
	private int width = 1024;
	private int height = 720;
	public int x = 0;
	public int y = 0;
	public boolean loop = true;
	public long speed;

	private Canvas canvas;
	private boolean pauseBool = false;

	public MovieClip(Bitmap[] bt) {
		this.bitmapList = bt;
	}

	// 接收图片bitmap数组
	private Bitmap[] bitmapList;

	// 热区，键为vcanvas对象，值为该对象移动的位置信息
	private Map<Vcanvas,Vector2[]> hotPoints = new HashMap<Vcanvas,Vector2[]>();
	
	public void addImageBitmap(Bitmap[] bt) {
		this.bitmapList = bt;
	}

	// 实现绘制方法
	public void drawSelfImplement(Canvas canvas) {
		this.canvas = canvas;
		loaderBitmap();
	}

	/*
	 * 循环播放图片
	 */
	private int currentPosition = -1;
	private int temp = 0;

	private void loaderBitmap() {
		currentPosition = temp;

		if (temp < bitmapList.length) {
			setDirtyRect(this.x, this.y, this.x + this.width, this.y
					+ this.height);

			Paint paint = new Paint();
			Rect src = new Rect(0, 0, bitmapList[currentPosition].getWidth(),
					bitmapList[currentPosition].getHeight());
			// 将要绘制在屏幕的什么个区域绘制上面截取出来的内容
			Rect dst = new Rect(x, y, x + width, y + height);
			// 这句就是在屏幕的0，0--250,250绘制整张图
			canvas.drawBitmap(bitmapList[currentPosition], src, dst, paint);
			
			// 绘制热区
			if(!hotPoints.isEmpty()){
				Iterator<Vcanvas> iter = hotPoints.keySet().iterator();
				while(iter.hasNext()){
					Vcanvas v = iter.next();
					Vector2[] vects = hotPoints.get(v);
					if(currentPosition < vects.length){
						v.Move(vects[currentPosition].getX(),vects[currentPosition].getY());
					}
				}
			}

			TimerTask tt = new TimerTask() {
				public void run() {
					if (!pauseBool) {
						temp++;
						Stage.getInstance().getView().postInvalidate();
					}
				}
			};
			Timer time = new Timer(true);
			time.schedule(tt, 5);
		} else {
			Rect src = new Rect(0, 0,
					bitmapList[currentPosition - 1].getWidth(),
					bitmapList[currentPosition - 1].getHeight());
			// 将要绘制在屏幕的什么个区域绘制上面截取出来的内容
			Rect dst = new Rect(x, y, x + width, y + height);
			// 这句就是在屏幕的0，0--250,250绘制整张图
			canvas.drawBitmap(bitmapList[currentPosition - 1], src, dst, paint);
			if (loop) {
				temp = 0;
				loaderBitmap();
			} else {
				return;
			}
		}

	}

	/*
	 * movieClip 暂停播放
	 */
	public void pause() {
		pauseBool = true;
	}

	/*
	 * movieClip 播放
	 */
	public void start() {
		pauseBool = false;
		temp = currentPosition + 1;
		loaderBitmap();
	}

	/*
	 * movieClip 停止
	 */
	public void stop() {
		pauseBool = true;
		// loaderBitmap();

	}

	// movieClip 设置屏幕大小
	public void screenSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public int currentFrame() {
		return currentPosition;
	}

	// movieClip 跳转到某一帧
	public void gotoFrame(int i) {

		this.pause();
		if (i >= 120) {
			temp = 0;
		} else if (i < 0) {
			temp = 119;
		} else {
			temp = i;
		}
		View v = Stage.getInstance().getView();
		if (v != null)
			v.postInvalidate();

	}
	
	public void addHotPoint(Hotpoint hp, Vector2[] vects){
		this.hotPoints.put(hp, vects);
	}
}
